package org.asset;

import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import oshi.software.os.OperatingSystem;

public class MainNext {
    public static void main(String[] args) {
        // 创建 SystemInfo 对象
        SystemInfo systemInfo = new SystemInfo();

        // 获取 CPU 信息
        CentralProcessor processor = systemInfo.getHardware().getProcessor();
        System.out.println("CPU Logical Processor Count: " + processor.getLogicalProcessorCount());
        System.out.println("CPU Physical Processor Count: " + processor.getPhysicalProcessorCount());
        System.out.println("CPU Vendor: " + processor.getProcessorIdentifier().getVendor());
        System.out.println("CPU Name: " + processor.getProcessorIdentifier().getName());

        // 获取内存信息
        GlobalMemory memory = systemInfo.getHardware().getMemory();
        System.out.println("Total Memory: " + formatBytes(memory.getTotal()));
        System.out.println("Available Memory: " + formatBytes(memory.getAvailable()));

        // 获取操作系统信息
        OperatingSystem os = systemInfo.getOperatingSystem();
        System.out.println("Operating System: " + os.toString());
        System.out.println("System Boot Time: " + os.getSystemBootTime());
    }

    // 格式化字节数为可读的格式
    private static String formatBytes(long bytes) {
        if (bytes < 1024) return bytes + " B";
        int exp = (int) (Math.log(bytes) / Math.log(1024));
        char pre = "KMGTPE".charAt(exp - 1);
        return String.format("%.1f %sB", bytes / Math.pow(1024, exp), pre);
    }
}

//已连接到地址为 ''127.0.0.1:54630'，传输: '套接字'' 的目标虚拟机
//SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
//SLF4J: Defaulting to no-operation (NOP) logger implementation
//SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
//Exception in thread "main" java.lang.IllegalStateException: Unmapped relationship: 7
//at com.sun.jna.platform.win32.WinNT$SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX.fromPointer(WinNT.java:3033)
//at com.sun.jna.platform.win32.Kernel32Util.getLogicalProcessorInformationEx(Kernel32Util.java:715)
//at oshi.driver.windows.LogicalProcessorInformation.getLogicalProcessorInformationEx(LogicalProcessorInformation.java:61)
//at oshi.hardware.platform.windows.WindowsCentralProcessor.initProcessorCounts(WindowsCentralProcessor.java:155)
//at oshi.hardware.common.AbstractCentralProcessor.(AbstractCentralProcessor.java:73)
//at oshi.hardware.platform.windows.WindowsCentralProcessor.(WindowsCentralProcessor.java:66)
//at oshi.hardware.platform.windows.WindowsHardwareAbstractionLayer.createProcessor(WindowsHardwareAbstractionLayer.java:60)
//at oshi.util.Memoizer$1.get(Memoizer.java:87)
//at oshi.hardware.common.AbstractHardwareAbstractionLayer.getProcessor(AbstractHardwareAbstractionLayer.java:68)
//at org.asset.MainNext.main(MainNext.java:14)